#Kiwi Editor is a framework for building WYSIWYG web editors.
#Copyright (C) 2011  Roosevelt de Lima Sardinha

#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#Contact the author at roosevelt1@gmail.com (Roosevelt) 


# == Schema Information
# Schema version: 20101204202135
#
# Table name: workspaces
#
#  id                 :integer         not null, primary key
#  current_profile_id :integer
#  created_at         :datetime
#  updated_at         :datetime
#

class Workspace < ActiveRecord::Base
  has_many :profiles, :through => :assoc_workspace_profiles
  has_many :assoc_workspace_profiles, :dependent => :destroy  
  has_many :property_boxes, :dependent => :destroy
  has_many :instruction_boxes, :dependent => :destroy
  has_many :tool_boxes, :dependent => :destroy
  has_many :edition_areas, :dependent => :destroy
  
  def update_with_json(json)
    as_json = ActiveSupport::JSON
    
    self.current_profile_id = json["current_profile"]
    self.save
    
    # Remove old associations
    AssocWorkspaceProfile.find_all_by_workspace_id(self.id).each do |assoc|
      AssocWorkspaceProfile.destroy(assoc);
    end
    
    # Save new associations
    json["profiles"].each do |profile|
      assoc_workspace_profile = self.assoc_workspace_profiles.build
      assoc_workspace_profile.profile_id = profile 
      assoc_workspace_profile.save
    end
    
    # Remove old instruction_boxes
    InstructionBox.find_all_by_workspace_id(self.id).each do |instruction_box|
      InstructionBox.destroy(instruction_box);
    end
    
    # Save new instruction_boxes
    json["instruction_boxes"].each do |instruction_box_data|
      instruction_box = self.instruction_boxes.build
      instruction_box.instruction_box_id = instruction_box_data["id"]
      instruction_box.parent_id = instruction_box_data["parent"]
      instruction_box.top = instruction_box_data["top"]
      instruction_box.left = instruction_box_data["left"]
      instruction_box.width = instruction_box_data["width"]
      instruction_box.height = instruction_box_data["height"]
      instruction_box.title = instruction_box_data["title"] 
      instruction_box.css_path = instruction_box_data["css_path"]
      instruction_box.save
    end
    
    # Remove old property_boxes
    PropertyBox.find_all_by_workspace_id(self.id).each do |property_box|
      PropertyBox.destroy(property_box);
    end
    
    # Save new property_boxes
    json["property_boxes"].each do |property_box_data|
      property_box = self.property_boxes.build
      property_box.property_box_id = property_box_data["id"]
      property_box.parent_id = property_box_data["parent"]
      property_box.top = property_box_data["top"]
      property_box.left = property_box_data["left"]
      property_box.width = property_box_data["width"]
      property_box.height = property_box_data["height"]
      property_box.title = property_box_data["title"] 
      property_box.css_path = property_box_data["css_path"]
      property_box.save
    end  
    
    # Remove old tool_boxes
    ToolBox.find_all_by_workspace_id(self.id).each do |tool_box|
      ToolBox.destroy(tool_box);
    end
    
    # Save new tool_boxes
    json["tool_boxes"].each do |tool_box_data|
      tool_box = self.tool_boxes.build
      tool_box.tool_box_id = tool_box_data["id"]
      tool_box.parent_id = tool_box_data["parent"]
      tool_box.top = tool_box_data["top"]
      tool_box.left = tool_box_data["left"]
      tool_box.width = tool_box_data["width"]
      tool_box.height = tool_box_data["height"]
      tool_box.title = tool_box_data["title"] 
      tool_box.css_path = tool_box_data["css_path"]
      tool_box.save
    end
  
    # Remove old edition_areaes
    EditionArea.find_all_by_workspace_id(self.id).each do |edition_area|
      EditionArea.destroy(edition_area);
    end
    
    # Save new edition_areas
    json["edition_areas"].each do |edition_area_data|
      edition_area = self.edition_areas.build
      edition_area.edition_area_id = edition_area_data["id"]
      edition_area.parent_id = edition_area_data["parent"]
      edition_area.top = edition_area_data["top"]
      edition_area.left = edition_area_data["left"]
      edition_area.width = edition_area_data["width"]
      edition_area.height = edition_area_data["height"]
      edition_area.title = edition_area_data["title"] 
      edition_area.css_path = edition_area_data["css_path"]
      edition_area.save
    end
  end
  
  def to_hash
    as_json = ActiveSupport::JSON
    
    all_workspace_data = {}
    all_workspace_data["id"] = self.id
    all_workspace_data["current_profile_id"] = self.current_profile_id

    # Preparing profiles data
    all_profiles_array = []
    self.profiles.all.each do |profile|      
      all_profiles_array << profile.id           
    end   
    all_workspace_data["profiles"] = all_profiles_array
    
    
    # Preparing instruction boxes data
    all_instruction_boxes_array = []
    self.instruction_boxes.all.each do |instruction_box| 
      instruction_box_data = {}
      instruction_box_data["id"] = instruction_box.instruction_box_id  
      instruction_box_data["parent"] = instruction_box.parent_id
      instruction_box_data["top"] = instruction_box.top
      instruction_box_data["left"] = instruction_box.left
      instruction_box_data["width"] = instruction_box.width
      instruction_box_data["height"] = instruction_box.height
      instruction_box_data["title"] = instruction_box.title
      instruction_box_data["css_path"] = instruction_box.css_path     
      all_instruction_boxes_array << instruction_box_data           
    end
    
    all_workspace_data["instruction_boxes"] = all_instruction_boxes_array
    
    # Preparing property boxes data
    all_property_boxes_array = []
    self.property_boxes.all.each do |property_box| 
      property_box_data = {}
      property_box_data["id"] = property_box.property_box_id  
      property_box_data["parent"] = property_box.parent_id
      property_box_data["top"] = property_box.top
      property_box_data["left"] = property_box.left
      property_box_data["width"] = property_box.width
      property_box_data["height"] = property_box.height
      property_box_data["title"] = property_box.title
      property_box_data["css_path"] = property_box.css_path     
      all_property_boxes_array << property_box_data           
    end
    
    all_workspace_data["property_boxes"] = all_property_boxes_array
    
    # Preparing toolboxes data
    all_tool_boxes_array = []
    self.tool_boxes.all.each do |tool_box| 
      tool_box_data = {}
      tool_box_data["id"] = tool_box.tool_box_id  
      tool_box_data["parent"] = tool_box.parent_id
      tool_box_data["top"] = tool_box.top
      tool_box_data["left"] = tool_box.left
      tool_box_data["width"] = tool_box.width
      tool_box_data["height"] = tool_box.height
      tool_box_data["title"] = tool_box.title
      tool_box_data["css_path"] = tool_box.css_path     
      all_tool_boxes_array << tool_box_data           
    end
    
    all_workspace_data["tool_boxes"] = all_tool_boxes_array
    
    # Preparing edition area data
    all_edition_areas_array = []
    self.edition_areas.all.each do |edition_area| 
      edition_area_data = {}
      edition_area_data["id"] = edition_area.edition_area_id  
      edition_area_data["parent"] = edition_area.parent_id
      edition_area_data["top"] = edition_area.top
      edition_area_data["left"] = edition_area.left
      edition_area_data["width"] = edition_area.width
      edition_area_data["height"] = edition_area.height
      edition_area_data["title"] = edition_area.title
      edition_area_data["css_path"] = edition_area.css_path     
      all_edition_areas_array << edition_area_data           
    end
    
    all_workspace_data["edition_areas"] = all_edition_areas_array
    
    return all_workspace_data
  end
  
end
